# -*- coding: utf-8 -*-
# standard
import os
import sys

# third
from fastapi import APIRouter
from fastapi import Depends
from fastapi import Query as QueryParam
from pydantic import BaseModel

# local
_P_PATH =  os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
if _P_PATH not in sys.path:
    sys.path.append(_P_PATH)
from toolbox.common_tools import logger
from toolbox.common_tools import INCLUDE_SCHEMA
from db.sqlalchemy_dbc import Resp
from functions import *


"""
@Title:  setting 模块
@File: demo_view.py
@Author: walle 2024年12日29日 10时33分35秒
@Version: 1.0.0
@Desc: 
"""


router = APIRouter(prefix="/v1/setting")
EXT = {"tags": ["设置"], "response_model": Resp, "include_in_schema": INCLUDE_SCHEMA}


@router.get("/notification_source/selector", summary="通知事件的来源选择器", **EXT)
async def notification_source_selector_view(
    authorization: str = Depends(check_instruction_authorization),
):
    return Resp(data=NotificationSource.get_selector())


@router.get("/notification_method/selector", summary="推送消息的方式的来源选择器", **EXT)
async def notification_method_selector_view(
    authorization: str = Depends(check_instruction_authorization),
):
    return Resp(data=NotificationMethodName.get_selector())


@router.post("/notification_strategy/add", summary="添加一个通知策略", **EXT)
async def demo_add_view(
    params: AddNotificationStrategyParams,
    authorization: str = Depends(check_instruction_authorization),
):
    resp = NotificationStrategyOrm.add(params=params)
    return resp


@router.post("/notification_strategy/edit", summary="编辑一个通知策略", **EXT)
async def demo_add_view(
    params: EditNotificationStrategyParams,
    authorization: str = Depends(check_instruction_authorization),
):
    resp = NotificationStrategyOrm.edit(params=params)
    return resp


@router.post("/notification_strategy/delete", summary="删除通知策略", **EXT)
async def notification_strategy_delete_view(
    params: CommonDeleteMicServiceParams,
    authorization: str = Depends(check_instruction_authorization),
):
    resp = NotificationStrategyOrm.delete(params=params)
    return resp


@router.get("/notification_method_name/selector", summary="推送方式选择器", **EXT)
async def notification_method_name_selector_view(
    authorization: str = Depends(check_instruction_authorization),
):
    return Resp(data=NotificationMethodName.get_selector())


@router.get("/notification_strategy/page", summary="分页查询通知策略", **EXT)
async def demo_page_view(
    id: int | None = QueryParam(default=None, description="策略id params.operator_customer_id"),
    keyword: str | None = QueryParam(default=None, description="关键字 策略名 模糊"),
    customer_id: int | None = QueryParam(default=None, description="客户id"),
    store_id: int | None = QueryParam(default=None, description="门店id"),
    method_name: str | None = QueryParam(default=None, description="推送方式名"),
    source_name: str | None = QueryParam(default=None, description="事件来源名"),
    page_num: int = QueryParam(default=1, description="页码"),
    page_size: int = QueryParam(default=10, description="每页条数"),
    authorization: str = Depends(check_instruction_authorization),
):
    """
    todo: 本接口即将被 /notification_strategy/page_plus 替代
    """
    return page_notification_strategies(
        id=id,
        customer_id=customer_id,
        keyword=keyword,
        store_id=store_id,
        method_name=method_name,
        source_name=source_name,
        page_num=page_num,
        page_size=page_size,
    )


class NotificationStrategyFilterParams(BaseModel):
    id: int | None = Field(default=None, description="策略id")
    customer_ids: list[int] | None = Field(default=None, description="客户ids") 
    store_ids: list[int] | None = Field(default=None, description="门店ids")
    keyword: str | None = Field(default=None, description="关键字 策略名 模糊")
    method_name: NotificationMethodName | None = Field(default=None, description="推送方式名 请使用选择器 /notification_method_name/selector")
    notification_target: str | None = Field(default=None, description="推送目标 精确匹配")
    source_name: NotificationSource | None = Field(default=None, description="事件来源 请使用选择器 /notification_source/selector")
    page_num: int = Field(default=1, description="页码")
    page_size: int = Field(default=10, description="每页条数")


@router.post("/notification_strategy/page_plus", summary="分页查询通知策略 相比这个接口支持更多的条件", **EXT)
async def demo_page_view(
    params: NotificationStrategyFilterParams,
    authorization: str = Depends(check_instruction_authorization),
):
    return await page_notification_strategies_async(**params.model_dump(exclude_none=True))


@router.post("/notification_target_comment/add", summary="添加一个通知目标的备注", **EXT)
async def notification_target_add_view(
    params: NotificationTargetCommentParams,
    authorization: str = Depends(check_instruction_authorization),
):
    resp = NotificationTargetCommentOrm.add(params=params)
    return resp


@router.post("/notification_target_comment/edit", summary="编辑一个通知目标的备注", **EXT)
async def notification_target_edit_view(
    params: NotificationTargetCommentParams,
    authorization: str = Depends(check_instruction_authorization),
):
    resp = NotificationTargetCommentOrm.edit(params=params)
    return resp


@router.post("/notification_target_comment/delete", summary="删除通知目标的备注", **EXT)
async def notification_target_delete_view(
    params: CommonDeleteMicServiceParams,
    authorization: str = Depends(check_instruction_authorization),
):
    resp = NotificationTargetCommentOrm.delete(params=params)
    return resp


@router.get("/notification_target_comment/page", summary="分页查询通知目标的备注", **EXT)
async def notification_target_page_view(
    id: int | None = QueryParam(default=None, description="目标id"),
    keyword: str | None = QueryParam(default=None, description="关键字 目标名 模糊"),
    source: NotificationSource | None = QueryParam(default=None, description="来源"),
    page_num: int = QueryParam(default=1, description="页码"),
    page_size: int = QueryParam(default=10, description="每页条数"),
    authorization: str = Depends(check_instruction_authorization),
):
    return NotificationTargetCommentOrm.page(id=id,
                                     keyword=keyword,
                                     source=source,
                                     page_num=page_num,
                                     page_size=page_size)


@router.get("/pushed_message/page", summary="分页查询以推送消息", **EXT)
async def pushed_message_page_view(
    keyword: str = QueryParam(None, max_length=64, description="关键词 模糊匹配门店名 消息内容"), 
    method: NotificationMethodName | None = QueryParam(None, description="推送方式 请使用选择器"),
    target: str | None = QueryParam(None, max_length=128, description="推送目标 精确匹配"),
    customer_id: int | None = QueryParam(None, description="客户ID"),
    store_id: int | None = QueryParam(None, description="门店ID"),
    start_time: datetime | None = QueryParam(None, description="开始时间"),
    end_time: datetime | None = QueryParam(None, description="结束时间"),
    success: int | None = QueryParam(None, description="消息推送是否成功"),
    page_num: int = QueryParam(1, ge=1, description="页码"),
    page_size: int = QueryParam(10, ge=1, le=100, description="每页条数"),
    authorization: str = Depends(check_instruction_authorization),
):
    """
    todo: 本接口即将被 /pushed_message/page_plus 替代
    """
    return page_send_records(keyword=keyword,
                             method=method,
                             target=target,
                             customer_id=customer_id,
                             store_id=store_id,
                             start_time=start_time,
                             end_time=end_time,
                             success=success,
                             page_num=page_num,
                             page_size=page_size,)


class PushMessageSettingPageParams(BaseModel):
    keyword: str | None = Field(None, max_length=64, description="关键词 模糊匹配门店名 消息内容")
    method: NotificationMethodName | None = Field(None, description="推送方式 请使用选择器")
    target: str | None = Field(None, max_length=128, description="推送目标 精确匹配")
    customer_ids: list[int] | None = Field(None, description="客户IDs")
    store_ids: list[int] | None = Field(None, description="门店IDs")
    start_time: datetime | None = Field(None, description="开始时间")
    end_time: datetime | None = Field(None, description="结束时间")
    success: int | None = Field(None, description="消息推送是否成功")
    page_num: int = Field(1, ge=1, description="页码")
    page_size: int = Field(10, ge=1, le=100, description="每页条数")



@router.post("/pushed_message/page_plus", summary="分页查询以推送消息", **EXT)
async def pushed_message_page_view(
    params: PushMessageSettingPageParams,
    authorization: str = Depends(check_instruction_authorization),
):
    return await page_send_records_async(**params.model_dump(exclude_none=True))


@router.get("/pushed_message/setting/summary", summary="推送消息统计", **EXT)
async def pushed_message_setting_summary_view(
    customer_id: int | None = QueryParam(None, description="客户ID"),
    authorization: str = Depends(check_instruction_authorization),
):
    return get_push_message_setting_summary(customer_id=customer_id)


if __name__ == '__main__':
    pass